<?php
$code = $_GET["code"];
$aid = $_GET["id"];
$asecret =$_GET["scr"];
$to =$_GET["to"];
$userInfoOpenid =getUserInfoOpenid($code,$aid,$asecret);
function getUserInfoOpenid($code,$aid,$asecret)
{
    $appid = $aid;
    $appsecret = $asecret;

    //根据code获得Access Token
    $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
    $access_token_json = https_request($access_token_url);
    $access_token_array = json_decode($access_token_json, true);
    $access_token = $access_token_array['access_token'];
    $openid = $access_token_array['openid'];
    return $openid;
}

function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
}
header('Location: ' . $to . '/openid/' . $userInfoOpenid ) ;


?>